﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1.Page
{
    public partial class FrameworkEdit : System.Web.UI.Page
    {
        string zx, fj, yh,id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            zx = Request.QueryString["zx"];
            fj = Request.QueryString["fj"];
            yh = Request.QueryString["yh"];
            if(!IsPostBack)
            {                
                if (id != "")
                {
                    结构分级Data d = new 结构分级Data();
                    d.ID = Guid.Parse(id);
                    DataTable dt = 结构分级DataDBOption.DataTableSelect(d);
                    DataRow dr = dt.Rows[0];
                    if(zx=="")
                    {
                        TextBox2.Text = dr["名称"].ToString();
                        TextBox2.Enabled = false;
                    }
                    else if (fj == "")
                    {
                        TextBox3.Text = dr["名称"].ToString();
                        TextBox3.Enabled = false;
                    }
                    else if (yh == "")
                    {
                        TextBox4.Text = dr["名称"].ToString();
                        TextBox4.Enabled = false;
                    }
                    TextBox6.Text = dr["地址"].ToString();
                    TextBox7.Text = dr["备注"].ToString();
                }
                else
                {
                    Button3.Visible = false;
                }
                if (zx == "")//添加中心
                {
                    Label1.Text = "中心";
                    Label3.Visible = false;
                    TextBox3.Visible = false;
                    Label4.Visible = false;
                    TextBox4.Visible = false;
                    Label5.Visible = false;
                    TextBox5.Visible = false;

                    Label8.Visible = false;
                    TextBox8.Visible = false;
                }
                else if (fj == "")//添加分局
                {
                    Label1.Text = "分局";
                    结构分级Data d = new 结构分级Data();
                    d.ID = Guid.Parse(zx);
                    DataTable dt = 结构分级DataDBOption.DataTableSelect(d);
                    DataRow dr = dt.Rows[0];
                    TextBox2.Text = dr["名称"].ToString();
                    TextBox2.Enabled = false;
                    Label4.Visible = false;
                    TextBox4.Visible = false;
                    Label5.Visible = false;
                    TextBox5.Visible = false;

                    Label8.Visible = false;
                    TextBox8.Visible = false;
                }
                else if (yh == "")//添加分局
                {
                    Label1.Text = "普通用户";
                    结构分级Data d = new 结构分级Data();
                    d.ID = Guid.Parse(zx);
                    DataTable dt = 结构分级DataDBOption.DataTableSelect(d);
                    DataRow dr = dt.Rows[0];
                    TextBox2.Text = dr["名称"].ToString();
                    TextBox2.Enabled = false;
                    d.ID = Guid.Parse(fj);
                    dt = 结构分级DataDBOption.DataTableSelect(d);
                    dr = dt.Rows[0];
                    TextBox3.Text = dr["名称"].ToString();
                    TextBox3.Enabled = false;
                    Label5.Visible = false;
                    TextBox5.Visible = false;

                    Label8.Visible = false;
                    TextBox8.Visible = false;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            结构分级Data d= new 结构分级Data();
            d.地址 = TextBox6.Text;
            d.备注 = TextBox7.Text;
            d.Creator = HttpContext.Current.User.Identity.Name;
            if (zx == "")
            {
                if (id == "")
                {
                    d.名称 = TextBox2.Text;
                    结构分级DataDBOption.Insert_zx(d);
                }
                else
                {
                    d.ID = Guid.Parse(id);
                    结构分级DataDBOption.Update_zx(d);
                }
                Response.Redirect("~/Page/FrameworkManage.aspx");
            }
            else if (fj == "")
            {
                if (id == "")
                {
                    d.名称 = TextBox3.Text;
                    d.中心ID = Guid.Parse(zx);
                    结构分级DataDBOption.Insert_fj(d);
                }
                else
                {
                    d.ID = Guid.Parse(id);
                    结构分级DataDBOption.Update_zx(d);
                }
                Response.Redirect("~/Page/FrameworkManage.aspx");
            }
            else if (yh == "")
            {
                if (id == "")
                {
                    d.名称 = TextBox4.Text;
                    d.中心ID = Guid.Parse(zx);
                    d.分局ID = Guid.Parse(fj);
                    结构分级DataDBOption.Insert(d);
                }
                else
                {
                    d.ID = Guid.Parse(id);
                    结构分级DataDBOption.Update_zx(d);
                }
                Response.Redirect("~/Page/FrameworkManage.aspx");
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            结构分级Data d = new 结构分级Data();
            d.ID = Guid.Parse(Request.QueryString["id"]);
            结构分级DataDBOption.Delete(d);
            Response.Redirect("~/Page/FrameworkManage.aspx");
        }
    }
}